package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.HR.batchpro.Contractitem;
import com.base.myproject.client.HR.batchpro.Healthproc_item;
import com.base.myproject.client.HR.batchpro.ProcItem;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;


public class Bcode_pro extends LayoutContainer{
	HorizontalPanel hpanel = new HorizontalPanel();
	BcodeSelectDialog<TreeNodeModelData> bsd_add = null;
	BaseComboBoxX C=new BaseComboBoxX("codename","mark","hr_salary_ploy_m", "");
	NumberField  Money =new NumberField();
	Button save = new Button("保存");
	public Bcode_pro()
	{
		init();
	}
	void init()
	{
		bsd_add = new BcodeSelectDialog<TreeNodeModelData>(){
			public void doposted(List<TreeNodeModelData> modeldatalist) {
				if(modeldatalist.size()>0)
				{
					this.setEmptyText("已经选择了数据");
					
					List<TreeNodeModelData>  selectchecklist=bsd_add.getTree().getCheckedSelection();
					
					if(selectchecklist.size()<=0)
					{
						MessageBox.alert("错误", "请选择要批量操作的人员", null	);
						return;
					}
					String bcodestr = "";
					for(TreeNodeModelData tnmd:selectchecklist)
					{
						BusiObject bo = new BusiObject();
						
						bcodestr +=" systembcode like '"+tnmd.get("systembcode")+"%25' or ";

					}
					bcodestr +=" 1=2";
					//GWT.log("bcodestr ="+bcodestr);
					
					
					String sql  = "select icode,bcode,bname,up_department, salaryploy,basesalary from bcode where exists (select * from systembcode_view where systembcode_view.bcode = bcode.bcode  and ( "+bcodestr+"))";
					//GWT.log("sql ="+sql);
					store.getStoreinfo().setSql(sql);
					store.reload();
				}
			}

		};
		bsd_add.setNeesreselect(false);
		bsd_add.setEmptyText("未选择数据");
		bsd_add.setExpanded();
		bsd_add.setCheckNodes(CheckNodes.BOTH);
		bsd_add.addListener(Events.Changed, getListener());
		
		
		
		initwidgetx();
		
	}
	private void initwidgetx() {
		hpanel.add(new LabelField("请选择人员:"));
		hpanel.add(bsd_add);
		hpanel.add(new LabelField("请选择薪金策略:"));
		
		
        String html = "<tpl for=\".\"><div class=\"x-combo-list-item\">{value}<h3><span><div style='white-space:normal; display:block; word-break:break-all'>{extdisplayname}</div></span></h3></div></tpl>";
		C.setTemplate(html);
		C.setMinListWidth(400);
		C.addListener(Events.Changed, getListener());
		C.setName("salaryploy");
		C.setAllowBlank(false);
		hpanel.add(C);
		
		hpanel.add(new LabelField("薪金:"));
		Money.addListener(Events.Changed, getListener());
		hpanel.add(Money);
		save.addListener(Events.OnClick, getListener());
		hpanel.add(save);
		
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(hpanel);
		vpanel.add(createGrid());
		add(vpanel);
		
	}
	
	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("up_department");
		type.getField("up_department").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("salaryploy");
		type.getField("salaryploy").setType(String.class);
		type.addField("basesalary");
		type.getField("basesalary").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("bcode");
		info.setKeyname(new String[] { "icode" });
		info.setNeedAllRow("true");
		info.setForcenolimit("true");

		store = new BaseStore<BusiObject>(type, info,false);
		return store;
	}
	
	
	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {              
		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {             
		    	  if(model.get(property) !=null && model.get(property) instanceof ModelData)
		    	  {
		    		  return ((ModelData)model.get(property)).get("value");
		    	  }                                                                            
		    	  return model.get(property);                                                     
		      }                                                                                 
		    }; 
		
		ColumnConfig C3 = new ColumnConfig("icode",	"icode", 50);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("bcode",	"雇员编码", 120);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("bname",	"雇员姓名", 120);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("up_department",	"up_department", 120);
		columns.add(C6);
		
		ColumnConfig C1 = new ColumnConfig("salaryploy","薪金策略", 120);
		C1.setRenderer(renderer);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("basesalary","薪金", 120);
		columns.add(C2);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SIMPLE);
		grid.setSelectionModel(cbs);
		Tools.setResize(grid, 0.99,0.95);
		return grid;
	}
	

	
	
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.OnClick) {                     
							if (be.getSource().equals(save)) {                            
								store.trySave();                                
							}                                                      
						} else if (be.getType() == Events.Changed) {              
							if (be.getSource().equals(C)) {
								int i = 0;
								if(C!=null)
								for(BusiObject bo:getStore().getModels())
								{
									grid.setValue(i,"salaryploy", C.getValue());
									i++;
								}

							}else if (be.getSource().equals(Money)) {
								int i = 0;
								if(Money!=null)
								for(BusiObject bo:getStore().getModels())
								{
									grid.setValue(i,"basesalary", Money.getValue());
									i++;
								}

							}
							
						} else if (be.getType() == Events.KeyUp) {               
							if (be instanceof ComponentEvent) {                    
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{                                                    
								}                                                    
							}                                                      
						}                                                        
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}        
	
	
	
	
	
		
}
